package com.os.opencv.java.chapter11;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class Harris {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        //读取图像并在屏幕上显示
        Mat src = Imgcodecs.imread("pics/go.png", Imgproc.COLOR_BGR2GRAY);
        Mat gray = new Mat();
        Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
        HighGui.imshow("gray", gray);
        HighGui.waitKey(0);

        //计算harris评价系数
        Mat dst = new Mat();
        int blockSize = 2;
        int ksize = 3;
        Imgproc.cornerHarris(gray, dst, blockSize, ksize, 0.04);

        //归一化并将数据类型转换为8位无符号数
        Core.normalize(dst, dst, 0, 255, Core.NORM_MINMAX);
        Core.convertScaleAbs(dst, dst);

        //寻找harris角点并用圆圈画出
        Mat result = src.clone();
        for(int row = 0; row<dst.rows(); row ++){
            for(int col = 0; col<dst.cols(); col++){
                double[] r = dst.get(row, col);
                if(r[0] > 125){
                    Imgproc.circle(result, new Point(col, row), 2, new Scalar(0,0,255));
                }
            }
        }
        HighGui.imshow("harris corners", result);
        HighGui.waitKey(0);
        System.exit(0);

    }
}
